﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using ReklamaWPF;
using System.Windows.Media;

namespace OutlookCalendar.Controls
{
    public class TimeslotPanel : Panel
    {
        public static DateTime StartOfWeek(DateTime dt, DayOfWeek startOfWeek)
        {
            int diff = dt.DayOfWeek - startOfWeek;
            if (diff < 0)
            {
                diff += 7;
            }

            return dt.AddDays(-1 * diff).Date;
        }
       

        #region StartTime

        /// <summary>
        /// StartTime Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty StartTimeProperty =
            DependencyProperty.RegisterAttached("StartTime", typeof(DateTime), typeof(TimeslotPanel),
                new FrameworkPropertyMetadata((DateTime)Functionality.GetFirstDateOfWeek(DateTime.Now, DayOfWeek.Monday)));

        /// <summary>
        /// Gets the StartTime property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static DateTime GetStartTime(DependencyObject d)
        {
            return (DateTime)d.GetValue(StartTimeProperty);
        }

        /// <summary>
        /// Sets the StartTime property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static void SetStartTime(DependencyObject d, DateTime value)
        {
            d.SetValue(StartTimeProperty, value);
        }

        #endregion

        #region EndTime

        /// <summary>
        /// EndTime Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty EndTimeProperty =
            DependencyProperty.RegisterAttached("EndTime", typeof(DateTime), typeof(TimeslotPanel),
                new FrameworkPropertyMetadata((DateTime)Functionality.GetLastDateOfWeek(DateTime.Now, DayOfWeek.Monday)));

        /// <summary>
        /// Gets the EndTime property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static DateTime GetEndTime(DependencyObject d)
        {
            return (DateTime)d.GetValue(EndTimeProperty);
        }

        /// <summary>
        /// Sets the EndTime property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static void SetEndTime(DependencyObject d, DateTime value)
        {
            d.SetValue(EndTimeProperty, value);
        }

        #endregion

        
        protected override Size MeasureOverride(Size availableSize)
        {
            // Calculate size based on duration?
            Size size = new Size(double.PositiveInfinity, double.PositiveInfinity);

            foreach (UIElement element in this.Children)
            {
                element.Measure(size);
            }

            return new Size();
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            double top = 0;
            double left = 0;
            double width = 0;
            double height = 0;

            foreach (UIElement element in this.Children)
            {
                Nullable<DateTime> startTime = element.GetValue(TimeslotPanel.StartTimeProperty) as Nullable<DateTime>;
                Nullable<DateTime> endTime = element.GetValue(TimeslotPanel.EndTimeProperty) as Nullable<DateTime>;

                double start_minutes = ((startTime.Value.Hour+18) * 60) + startTime.Value.Minute;
                double end_minutes = ((endTime.Value.Hour+18) * 60) + endTime.Value.Minute;
                double start_offset = (finalSize.Height / ((24) * 60)) * start_minutes;
                double end_offset = (finalSize.Height / ((24) * 60)) * end_minutes;

                top = start_offset+1;

                width = finalSize.Width;
                height = (end_offset - start_offset)-2;

                if (top >6335)
                {
                    start_minutes = ((startTime.Value.Hour-6) * 60) + startTime.Value.Minute;
                    end_minutes = ((endTime.Value.Hour-6) * 60) + endTime.Value.Minute;
                    start_offset = (finalSize.Height  / ((24) * 60)) * start_minutes;
                    end_offset = (finalSize.Height / ((24) * 60)) * end_minutes;

                    top = start_offset + 1;

                    width = finalSize.Width;
                    height = (end_offset - start_offset) - 2;
                }

                if (height < 0)
                {
                    start_minutes = ((startTime.Value.Hour - 6) * 60) + startTime.Value.Minute;
                    end_minutes =  ((endTime.Value.Hour +18) * 60) + endTime.Value.Minute;
                    start_offset = (finalSize.Height / ((24) * 60)) * start_minutes;
                    end_offset = (finalSize.Height / ((24) * 60)) * end_minutes;

                    top = start_offset + 1;

                    width = finalSize.Width;
                    height = (end_offset - start_offset) - 2;
                }

                if ( height < 15)
                {
                  //  ((OutlookCalendar.Controls.CalendarAppointmentItem)element).Height = 20;

                    foreach (ContentPresenter cpi in Functionality.FindVisualChildren<ContentPresenter>((OutlookCalendar.Controls.CalendarAppointmentItem)element))
                    {
                        //TextBlock tx = (TextBlock)cpi;
                        cpi.SetValue(TextBlock.FontSizeProperty, 8.0);
                       // ((Border)cpi.Parent).Background = System.Windows.Media.Brushes.Aqua;
                    }
                }
                element.Arrange(new Rect(left, top, width, height));
            }

            return finalSize;
        }

        protected override void OnRender(System.Windows.Media.DrawingContext dc)
        {
            base.OnRender(dc);
            
            //System.Windows.Media.BrushConverter bc = new System.Windows.Media.BrushConverter();
            //System.Windows.Media.Brush brush = (System.Windows.Media.Brush)bc.ConvertFrom("#F1F5E3");
           // brush.Freeze();
            //this.Background = brush;
            //this.InvalidateVisual();
        }

        protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            try
            {
                Border br = ((Border)((ContentPresenter)e.Source).Parent);
                OutlookCalendar.Controls.CalendarAppointmentItem ai = (OutlookCalendar.Controls.CalendarAppointmentItem)VisualTreeHelper.GetParent(br);
                OutlookCalendar.Controls.TimeslotPanel tp = (OutlookCalendar.Controls.TimeslotPanel)VisualTreeHelper.GetParent(ai);
                System.Windows.Controls.ItemsPresenter ip = (System.Windows.Controls.ItemsPresenter)VisualTreeHelper.GetParent(tp);
                System.Windows.Controls.Grid gd = (System.Windows.Controls.Grid)VisualTreeHelper.GetParent(ip);
                Border br1 = (Border)VisualTreeHelper.GetParent(gd);
                OutlookCalendar.Controls.CalendarDay cd = (OutlookCalendar.Controls.CalendarDay)VisualTreeHelper.GetParent(br1);
                System.Windows.Controls.Grid gd1 = (System.Windows.Controls.Grid)VisualTreeHelper.GetParent(cd);
                System.Windows.Controls.ScrollContentPresenter sp = (System.Windows.Controls.ScrollContentPresenter)VisualTreeHelper.GetParent(gd1);
                System.Windows.Controls.Grid gd2 = (System.Windows.Controls.Grid)VisualTreeHelper.GetParent(sp);
                System.Windows.Controls.ScrollViewer sw = (System.Windows.Controls.ScrollViewer)VisualTreeHelper.GetParent(gd2);
                System.Windows.Controls.Grid gd3 = (System.Windows.Controls.Grid)VisualTreeHelper.GetParent(sw);
                Border br2 = (Border)VisualTreeHelper.GetParent(gd3);
                OutlookCalendar.Controls.Calendar cl = (OutlookCalendar.Controls.Calendar)VisualTreeHelper.GetParent(br2);
                foreach (ContentPresenter cpi in Functionality.FindVisualChildren<ContentPresenter>(cl))
                {
                    try
                    {
                        var bc = new BrushConverter();
                        ((Border)cpi.Parent).Background = (Brush)bc.ConvertFrom("#F1F5E3");

                    }
                    catch (Exception ex) { }
                }
            }
            catch (Exception ex) { }

           // DependencyObject visualParent = VisualTreeHelper.GetParent(br2);
            if (e.RightButton == System.Windows.Input.MouseButtonState.Released)
            {
                try
                {
                    ((Border)((ContentPresenter)e.Source).Parent).Background = System.Windows.Media.Brushes.Aqua;
                }
                catch (Exception ex) { }
            }
           // MessageBox.Show(e.Source.ToString());
        }

       

    }
}
